 

function equipmentInspectionCtrl($scope, $http, ngDialog, paFetchService) {
  'ngInject';

  this.equipmentInspectionItemList = [];
  //  分页查询信息
  this.queryData = {};
  this.paginationData = {
    pageNum: 1,
    pageSize: 10,
    total: '',
  };
  //更换页码时
  $scope.pageChange = () => {
    $http.post(`${SERVER_CONTEXT}/api/devicecheckrecord/queryCheckRecordList`,
        angular.extend(this.queryData, this.paginationData)
        )
        .then((resp) => {
          //console.log(resp);
          this.equipmentInspectionItemList = resp.data.data;
          this.paginationData.total = resp.data.total;
        });
  };
  this.equipInformation = () => {
    $http.post(`${SERVER_CONTEXT}/api/devicecheckrecord/queryCheckRecordList`,
        angular.extend(this.queryData, this.paginationData)).
    then((resp) => {
      //console.log(resp.data.data);
      this.equipmentInspectionItemList = resp.data.data;
      this.paginationData.total = resp.data.total;
    });
  };

  // 初始化页面
  this.initPage = () => {
    // 获得标段list
    paFetchService
      .getSectionList()
      .then((resp) => {
        this.sectionList = resp.data;
      });
    this.equipInformation();
  }

  this.initPage();
  
  // 根据条件查询
  this.searchInformation = (param) =>{
    if (!param.startTime.$invalid && param.endTime.$invalid) {
      layer.alert('请填写检测结束时间');
      return false;
    } else if (param.startTime.$invalid && !param.endTime.$invalid) {
      layer.alert('请填写检测开始时间');
      return false;
    } else if (param.startTime.$valid && param.endTime.$valid) {
      if (param.endTime.$viewValue > param.startTime.$viewValue) {
        this.equipInformation();
      } else {
        layer.alert('检测开始时间应小于结束时间');
      }
    } else {
      this.equipInformation();
    }    
  };

  // 重置查询
  this.reset = () =>{
    this.queryData = {};
  }

  // 查看检测记录
  this.queryInformation = function(id){
    $scope.id = id;
    ngDialog.open({
      template: 'queryEquipmentInspection',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      scope: $scope,
      controllerAs: 'vm',
      controller: function( $http, $scope) {
        'ngInject';
        let str = `${SERVER_CONTEXT}/api/devicecheckrecord/selectCheckRecordInfo/${$scope.id}`;
        $http.get(str).
        then((resp) => {
          this.editInfo = resp.data;
        });

        // 获得头像src
        this.getAvatarSrc = (file) => {
          console.log(file);
          if (this.editInfo && file && file.filePath) {
            let src = `${SERVER_IP}/${file.filePath}`;
            return src;
          }
        };

        // 文件下载
        this.getDownloadSrc = (file) => {
          return `${SERVER_IP}${SERVER_CONTEXT}/api/devicecheckrecord/downloadDeviceInfo/${file.id}`;
        }
      },
    });
  }
  
  // 新增检测记录
  this.newTestCode = function(){
    ngDialog.open({
      template: 'equipmentInspection',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      scope: $scope,
      controllerAs: 'addTextCode',
      controller: function( $http, $scope) {
        'ngInject';
        this.addItem = {};
        this.attachments = [];
        this.submit = (param) => {
          // 修改表单是否提交过状态
          param.$submitted = true;

          // 表单校验
          if (param.$invalid) {
            return false;
          }

          // accessory 属性用于接收附件token 多个附件以逗号分隔
          this.addItem.accessory = this.attachments.map(item => item.id).join(',');
          $http.post(`${SERVER_CONTEXT}/api/devicecheckrecord/insertCheckRecord`,
              this.addItem)
              .then((resp) => {
                if (resp.data.errorCode == '0') {
                  layer.msg(resp.data.message);
                  $scope.equipmentInspection.equipInformation();
                } else {
                  layer.alert(resp.data.message);
                }
                $scope.closeThisDialog();
              });
        }
      },
    }); 
  }

  // 编辑检测信息
  this.editInformation = (id) => {
    $scope.id = id;
    ngDialog.open({
      template: 'editequipment',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      scope: $scope,
      controller: function($scope) {
        'ngInject';
        let str = `${SERVER_CONTEXT}/api/devicecheckrecord/selectCheckRecordInfo/${$scope.id}`;
        $http.get(str).
        then((resp) => {
          $scope.editInfo = resp.data;
          console.log( $scope.editInfo);
        });
        $scope.submit = (param) => {
          // 修改表单是否提交过状态
          param.$submitted = true;

          // 表单校验
          if (param.$invalid) {
            return false;
          }
          
          $scope.editInfo.accessory =
            $scope.editInfo.fileBeans.map(item => item.id).join(',');
          $http({
            method: 'PATCH',
            url: `${SERVER_CONTEXT}/api/devicecheckrecord/updateCheckRecord`,
            data: $scope.editInfo,
          }).then((resp) => {
            if (resp.statusText == 'OK') {
              $scope.equipmentInspection.equipInformation();
              resp.data.message == '更新设备检验记录成功' ? layer.msg("更新设备检验记录成功"):  layer.alert("更新设备检验记录失败");
              $scope.closeThisDialog();
            } else {
              $scope.closeThisDialog();
            }
          });
        }
      }
    });
  }

  // 删除检测信息
  this.deleteInformation = (id) => {
    let _this = this;
    $scope.modalTitle = '删除信息';
    $scope.modalContent = '确认要删除此信息吗?';
    ngDialog.openConfirm({
      template: 'paConfirm',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
    }).
    then((value) => {
      let reqData = [];
      reqData.push(id);
      $http({
        method: 'DELETE',
        url: `${SERVER_CONTEXT}/api/devicecheckrecord/deleteCheckRecord/${id}`
      }).
      then((resp) => {
        if(resp.data.success){
          layer.msg(resp.data.message);
          this.equipInformation();
        }else{
          layer.msg(resp.data.message);
        }
        
      });
    }, (reason) => {});
  };

  // 升序、降序
  this.up = (param,index) => { 
    this.queryData.columeName = param;    
    this.queryData.order = index;
    this.equipInformation();
  }

}

export default equipmentInspectionCtrl;